package com.khshop.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.khshop.pojo.User;
import com.khshop.service.UserService;
import com.khshop.vo.AjaxResult;
import com.khshop.vo.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 微信用户 前端控制器
 * </p>
 *
 * @author zhaoyang
 * @since 2020-10-09
 */
@Api(tags = "微信用户 前端控制器")
@RestController
@RequestMapping("/")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation("条件分页查询微信用户")
    @PostMapping("/findAllUser")
    @ResponseBody
    public AjaxResult findAllUser(Map<String,Object> map, @RequestParam(value = "page",defaultValue = "1") Integer page, @RequestParam(value = "limit",defaultValue = "1") Integer limit) {
        IPage<User> allUser = userService.findAllUser(map, page, limit);
        if (allUser != null) {
            return AjaxResult.success((int) allUser.getTotal(), allUser.getRecords());
        } else {
            return AjaxResult.error("连接超时，请稍后重试！");
        }
    }

    @ApiOperation("增加微信用户")
    @PostMapping("/addUser")
    @ResponseBody
    public AjaxResult addUser(@RequestBody User user) {
        
        if (userService.addUser(user)) {
            return  AjaxResult.success("增加成功");
        } else {
            return  AjaxResult.error("增加失败，请稍后重试！");
        }
    }

    @ApiOperation("修改微信用户")
    @PostMapping("/updateUser")
    @ResponseBody
    public AjaxResult updateUser(@RequestBody User user) {
        
        if (userService.updateUser(user)) {
            return  AjaxResult.success("修改成功");
        } else {
            return AjaxResult.error("修改失败，请稍后重试！");
        }
    }

    @ApiOperation("删除微信用户")
    @PostMapping("/delUser/{id}")
    @ResponseBody
    public AjaxResult delUser(@PathVariable Long id) {
        
        if (userService.delUser(id)) {
            return   AjaxResult.success("删除成功");
        } else {
            return AjaxResult.error("删除失败，请稍后重试！");
        }
    }

    @ApiOperation("根据用户编号查看指定用户详细信息")
    @PostMapping("/findUserById/{id}")
    @ResponseBody
    public AjaxResult findUserById(@PathVariable Long id) {
        AjaxResult.success("查询成功");
        User user = userService.findUserById(id);
        return AjaxResult.success("查询成功",user);
    }

    @ApiOperation("通过openid登录")
    @PostMapping("/findUserByOpenId/{openid}")
    @ResponseBody
    public AjaxResult findUserByOpenId(@PathVariable Long openid) {
        User user = userService.findUserByOpenId(openid);
        return  AjaxResult.success("查询成功",user);
    }


}

